name: Javascript

on:
  push:
    branches:
      - "develop"
  pull_request:
    types: [opened, synchronize, reopened]
  # Allows workflow to be called from other workflows
  workflow_call:
    inputs:
      ref:
        required: true
        type: string

# Avoid duplicate workflows on same branch
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}-javascript
  cancel-in-progress: true

jobs:
  js_test:
    runs-on: ubuntu-latest

    defaults:
      run:
        shell: bash --login -eo pipefail {0}

    steps:
      - name: Checkout Streamlit code
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.ref }}
          persist-credentials: false
          submodules: "recursive"
          fetch-depth: 2
      - name: Set Python version vars
        uses: ./.github/actions/build_info
      - name: Set up Python ${{ env.PYTHON_MAX_VERSION }}
        uses: actions/setup-python@v5
        with:
          python-version: "${{ env.PYTHON_MAX_VERSION }}"
      - name: Setup virtual env
        uses: ./.github/actions/make_init
      - name: Run make develop
        run: make develop
      - name: Run make protobuf
        run: make protobuf
      - name: Run make frontend-lib
        run: make frontend-lib
      - name: Audit frontend licenses
        run: ./scripts/audit_frontend_licenses.py
      - name: Run linters
        run: |
          # Run eslint as a standalone command to generate the test report.
          # We need to --hook-stage manual to trigger Typecheck too
          PRE_COMMIT_NO_CONCURRENCY=true SKIP=eslint pre-commit run --hook-stage manual --show-diff-on-failure --color=always --all-files
          # Run eslint using Makefile omitting the pre-commit
          make jslint
      - name: Validate NOTICES
        run: |
          # Run `make notices`. If it results in changes, warn the user and fail.
          make notices

          git_status=$(git status --porcelain -- NOTICES)
          if [[ -n $git_status ]]; then
            echo "::error::The NOTICES file is out of date! Please run \`make notices\` and commit the result."
            echo "::group::git diff NOTICES"
            git diff NOTICES
            echo "::endgroup::"
            exit 1
          else
            echo "NOTICES is up to date."
          fi
      - name: Run frontend tests
        run: make jstest

  components_lib_tests:
    runs-on: ubuntu-latest

    defaults:
      run:
        shell: bash --login -eo pipefail {0}

    steps:
      - name: Checkout Streamlit code
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.ref }}
          persist-credentials: false
          submodules: "recursive"
          fetch-depth: 2
      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: "20.x"
          cache: "yarn"
          cache-dependency-path: "component-lib/yarn.lock"
      - name: Install node dependencies
        working-directory: component-lib
        run: yarn install
      - name: Run frontend tests
        working-directory: component-lib
        run: yarn test
      - name: Build package
        working-directory: component-lib
        run: yarn build
